<template>
  <div class="container">
    <div>
      <el-date-picker
        v-model="params.date"
        size="small"
        type="month"
        change="onChangeDate"
        format="yyyy-MM"
        value-format="yyyy-MM"
        placeholder="选择日期"
      >
      </el-date-picker>
    </div>
    <el-calendar v-model="calendarValue">
      <template slot="dateCell" slot-scope="{ date, data }">
        <el-popover
          popper-class="popover-bgcolor"
          v-if="data.day.split('-')[1] === '12'"
          width="400"
          placement="top"
          trigger="click"
        >
          <div class="popover-bgc">
            <div style="color: #fff; font-size: 12px">{{ data.day }}</div>
            <div class="one">
              岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20
            </div>
            <div class="two">岳麓区20...</div>
            <div class="three">岳麓区20...</div>
          </div>
          <div slot="reference">
            <div>{{ data.day.split('-')[2] }}</div>
            <div class="one">
              岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20岳麓区20
            </div>
            <div class="two">岳麓区20...</div>
            <div class="three">岳麓区20...</div>
          </div>
        </el-popover>
        <div v-else>
          <div>{{ data.day.split('-')[2] }}</div>
        </div>
      </template>
    </el-calendar>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="items.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
    <!-- 弹出框 -->
    <el-dialog
      :title="addDialogTitle"
      :visible.sync="isVisibleEdit"
      width="50%"
    >
      <el-form ref="form" :model="form" label-width="100px" class="flex">
        <el-form-item label="检测对象：" class="wid50">
          <el-input
            v-model="form.LX"
            placeholder="请输入"
            class="handle-input mr10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分值占比：" class="wid50">
          <el-input
            v-model="form.ZB"
            placeholder="请输入"
            class="handle-input mr10"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import '@/assets/css/common.css'

import export2Excel from '@/utils/exportExcel/index.js'
import OSS from 'ali-oss';
import { time } from 'echarts';
export default {
  data () {

    return {
      calendarValue: null,
      params: {
        date: null
      },

      clientData: [],
      currentPage: 1, //初始页
      pageSize: 10,    //    每页的数据
      originalData: [],
      items: [
        { LX: '快车道', ZB: '3克' },
        { LX: '慢车道', ZB: '4克' },
        { LX: '人行道', ZB: '5克' },
      ],
      select: {
        CPH: ''
      },
      isVisibleEdit: false,
      form: {
        WaterCPH: "",
        WaterTime: "",
        CreateTime: "",
        WaterNumber: "",
        WaterLocation: "",
        WaterCphType: "",
        WaterPeople: "",
        WaterPeopleID: "",
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近六个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一年',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
            picker.$emit('pick', [start, end]);
          }
        },]
      },
      value2: ''
    }
  },
  created () {
    this.params.date = this.onFilterDate();
    if (localStorage.getItem('CTYPE') == '0') {
      this.rolelevel.shift()
    } else if (localStorage.getItem('CTYPE') != '-1') {
      this.rolelevel = []
    }
    this.getClientRoleList()
  },
  mounted () {
    this.getlistdata()
    this.getClientRoleList()
    this.screeningData()
  },
  methods: {
    onFilterDate (date) {
      let myDate = date || new Date();
      let y = myDate.getFullYear();    //获取完整的年份(4位,1970-????)
      let m = myDate.getMonth() + 1;       //获取当前月份(0-11,0代表1月)
      m = m > 9 ? m : `0${m}`;
      let d = myDate.getDate();        //获取当前日(1-31)
      d = d > 9 ? d : `0${d}`;
      return `${y}-${m}-${d}`;
    },
    onChangeDate () {
      this.calendarValue = this.params.date;
    },


    // 导入Excel
    onChange (file, fileList) {
      //  console.log(fileList);
      this.fileData = file; // 保存当前选择文件
      this.readExcel(); // 调用读取数据的方法
    },
    // 读取数据
    readExcel (e) {
      let that = this;
      const files = that.fileData;
      console.log(files);
      if (!files) {
        //如果没有文件
        return false;
      } else if (!/\.(xls|xlsx)$/.test(files.name.toLowerCase())) {
        this.$message.error("上传格式不正确，请上传xls或者xlsx格式");
        return false;
      }
      const fileReader = new FileReader();
      console.log(fileReader)
      fileReader.onload = ev => {
        try {
          const data = ev.target.result;
          //  console.log(ev.target)
          //  console.log(this);
          const workbook = this.XLSX.read(data, {
            type: "binary"// 以字符编码的方式解析
          });

          if (workbook.SheetNames.length >= 1) {
            //  this.$message({
            //    message: "导入数据表格成功",
            //    showClose: true,
            //    type: "success"
            //  });
            const wsname = workbook.SheetNames[0]; //取第一张表
            const ws = this.XLSX.utils.sheet_to_json(workbook.Sheets[wsname]); //生成json表格内容
            console.log(ws)
            this.inputExcelDataLength = ws.length
            this.fullscreenLoading = this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            })
            for (let i = 0, len = ws.length; i < len; i++) {
              var item = ws[i]
              //   var sheetData = {
              //     VID: item['居民电话'],
              //     RUBBISH_NAME: item["垃圾品名"],
              //     WEIGHT: item["垃圾称重"],
              //     photo: '',
              //     LONGITUDE: 0,
              //     LATITUDE: 0
              //   }
              //   let exceldata =  []
              //   exceldata.push(sheetData)
              this.addWeight(item)
            }
          } else {
            this.$message.error("未读取到表格内容");
          }
        } catch (e) {
          return false;
        }
      };
      // 如果为原生 input 则应是 files[0]
      fileReader.readAsBinaryString(files.raw);
    },
    async addWeight (item) {
      this.items.splice(3, 0, { LX: item['检测对象'], ZB: item['分值占比'] })
      this.$message.success('表格文件导入成功');
      this.fullscreenLoading.close()
    },
    // 导出Excel
    exportExcel () {
      let excelName = '检测对象记录'
      const tHeader = ['检测对象', '分值占比']
      const filterVal = ['LX', 'ZB']
      export2Excel(tHeader, filterVal, this.items, excelName)
    },
    // 打开新增窗口
    handleAdd () {
      this.addDialogTitle = '新增检测对象信息'
      //清空编辑框
      this.form = {
        LX: "",
        ZB: "",
      }
      //展示窗口
      this.isVisibleEdit = true;
      // this.idEdit = false;
    },
    // 打开编辑窗口
    handleEdit (index, row) {
      this.form = row;
      this.addDialogTitle = '编辑检测对象信息'
      this.isVisibleEdit = true;
    },
    // 点击保存，判断是新增还是编辑操作
    handleSubmit () {
      if (this.addDialogTitle == '新增检测对象信息') {
        this.saveAdd()
      } else {
        this.saveEdit()
      }
    },
    // 新增保存
    async saveAdd () {
      this.$set(this.items, 3, { LX: this.form.LX, ZB: this.form.ZB })
      this.items.splice(3, 0)
      this.$message.success('新增成功');
      this.isVisibleEdit = false
    },
    // 保存编辑
    async saveEdit () {
      this.items.LX = this.form.LX
      this.items.ZB = this.form.ZB
      this.$message.success('编辑成功');
      this.isVisibleEdit = false
    },
    // 删除车辆信息
    async handleDelete (index, row) {
      const confirmResult = await this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult == 'confirm') {
        this.items.splice(row.ID - 1, 1)
        this.$message.success('删除成功')
        this.isVisibleEdit = false
      } else {
        this.$message.info('取消删除')
        this.isVisibleEdit = false
      }

    },
    // 模糊查询
    screeningData () {
      if (this.select.name) this.items = this.items.filter(item => item.LX.includes(this.select.name))
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      // this.getlistdata();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize)
    }
  }
}
</script> 

<style>
 .align-items{
  display: flex;
  align-items: center;
}
.header{
  padding: 20px 20px 0 ;
  background-color: #fff;
  justify-content: space-between;
}
.icon-a{
  width: 20px;
  height: 20px;
}
.title{
  color: #101010;
  font-size: 16px;
}

.one{
  height: 20px;
  padding: 0 4px;
  line-height: 20px;
  border-left: 3px solid #3797FF;
  background: #DBECFF;
  font-size: 12px;
  margin-top: 6px;
  overflow:hidden; 
  text-overflow:ellipsis; 
  white-space:nowrap;
}
.two{
  height: 20px;
  line-height: 20px;
  padding: 0 4px;
  border-left: 3px solid #FF6161;
  background: #FFE1E1;
  font-size: 12px;
  margin-top: 6px;
  overflow:hidden; 
  text-overflow:ellipsis; 
  white-space:nowrap;
}
.three{
  height: 20px;
  line-height: 20px;
  font-size: 12px;
  padding: 0 4px;
  border-left: 3px solid #4EC95B;
  background: #E4FFE6;
  margin-top: 6px;
  overflow:hidden; 
  text-overflow:ellipsis; 
  white-space:nowrap;
}

.popover-bgc {
  background-color: #000 !important; 
  padding: 12px;
  border-radius: 6px;
}
.popover-bgc .one,
.popover-bgc .two,
.popover-bgc .three{
  background-color: #000 !important;
  color: #fff; 
}
.popover-bgcolor {
  padding: 0 !important;
}
.calendar_class .el-calendar-table:not(.is-range) td.next,
.calendar_class .el-calendar-table:not(.is-range) td.prev {
  pointer-events: none;
}
.calendar_class .el-calendar__header {
  display: none !important;
}

.calendar_class .el-calendar__body {
  padding: 0 20px 35px;
}

.calendar_class .el-calendar-day {
  height: 120px !important;
}

.el-popper[x-placement^='top'] .popper__arrow::after {
  border-top-color: #000 !important;
}

.content {
  overflow-y: scroll;
}
.el-col-6 {
  width: 350px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
.el-form-item label {
  width: 80px;
}
</style>